package com.ruitu.mall_98_2.mine.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyRefreshBaseFragment;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.ScreenUtils;
import com.beanu.arad.utils.SizeUtils;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.utils.UIUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.mine.ZujiBean;
import com.ruitu.router_module.bean.product.Product;
import com.ruitu.router_module.utils.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class FragmentFootprint extends MyRefreshBaseFragment {

    private LRecyclerView recyclerView;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private DataAdapter adapter;
    private List<ZujiBean> zujiBeans;
    private List<Integer> skipId = new ArrayList<>();

    public static FragmentFootprint newInstance() {

        Bundle args = new Bundle();

        FragmentFootprint fragment = new FragmentFootprint();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getInflate() {
        return R.layout.fragment_mine_three;
    }

    @Override
    protected LRecyclerView getRecyclerView(View view) {
        recyclerView = view.findViewById(R.id.lRecyclerView);
        return recyclerView;
    }

    @Override
    protected ProgressLayout findProgressLayout(View view) {
        return view.findViewById(R.id.progress);
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new DataAdapter(getContext());
        return adapter;
    }

    @Override
    protected void reqFirst() {

    }

    @Override
    protected void reqMore() {

    }

    @Override
    protected void reReq() {

    }

    @Override
    protected void initRecyclerView(View view) {
        super.initRecyclerView(view);

        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                getFootPrint();
            }
        });
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Product product = new Product();
                product.setGoodId(zujiBeans.get(position).getGoodsId());
                Bundle bundle = new Bundle();
                bundle.putSerializable("product", product);
                try {
                    A_Rutils.startActivity(getContext(), "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        getFootPrint();
    }

    class DataAdapter extends ListBaseAdapter<ZujiBean> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_commodity;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
//            ImageView icon = holder.getView(R.id.icon);
//            TextView tv_name = holder.getView(R.id.tv_name);
//            TextView tv_price = holder.getView(R.id.tv_price);
//            RelativeLayout rl_item = holder.getView(R.id.rl_item);
//            final ZujiBean zujiBean = mDataList.get(position);
//            Glide.with(mContext).load(Config.BASE_URL + zujiBean.getImageUrl()).apply(Arad.opts).into(icon);
//            tv_name.setText(zujiBean.getGoodsName());
//            tv_price.setText("￥" + zujiBean.getPrice());
//
//            rl_item.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Product product = new Product();
//                    product.setGoodId(zujiBean.getGoodsId());
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable("product", product);
//                    try {
//                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
//                    } catch (ClassNotFoundException e) {
//                        e.printStackTrace();
//                    }
//                }
//            });

            RelativeLayout rl_item = holder.getView(R.id.rl_item);
            ImageView icon = holder.getView(R.id.icon);
            TextView tv_pro_name = holder.getView(R.id.tv_pro_name);//商品名称
            TextView tv_price = holder.getView(R.id.tv_price);//现价
            TextView tv_price_del = holder.getView(R.id.tv_price_del);//原价
            final ZujiBean pro = mDataList.get(position);

            int width = ScreenUtils.getScreenWidth();
            int itemWidth = (width - (SizeUtils.dp2px(40))) / 2;
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) icon.getLayoutParams();
            params.width = itemWidth;
            params.height = itemWidth;
            icon.setLayoutParams(params);

            GlideUtil.loadImg(Config.BASE_URL + pro.getImageUrl(), icon);
            tv_pro_name.setText(pro.getGoodsName());
            tv_price.setText("￥" + pro.getPrice());
            tv_price_del.setText("￥" + pro.getPrice());
            UIUtils.setDelLine(tv_price_del);

            TextView tv_sale_num = holder.getView(R.id.tv_sale_num);//销量
            if (null != tv_sale_num) {
                tv_sale_num.setText("销量" + (pro.getGoodVolume() > 999 ? 999 + "+" : pro.getGoodVolume() + ""));
            }

            rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Product product = new Product();
                    product.setGoodId(pro.getGoodsId());
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("product", product);
                    try {
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ProductDetailActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });

            //下面是设置标签的,只适用于部分页面
            TextView tag_zidingyi = holder.getView(R.id.tag_zidingyi);//自定义
            TextView tag_pintuan = holder.getView(R.id.tag_pintuan);//拼团
            TextView tag_qujianjia = holder.getView(R.id.tag_qujianjia);//区间价
            TextView tag_yuding = holder.getView(R.id.tag_yuding);//预订
            TextView tag_yushou = holder.getView(R.id.tag_yushou);//预售

            try {
                if (!TextUtils.isEmpty(pro.getGoodTagName())) {//是否有自定义标签
                    tag_zidingyi.setVisibility(View.VISIBLE);
                    tag_zidingyi.setText(pro.getGoodTagName());//设置自定义标签名称
                } else {
                    tag_zidingyi.setVisibility(View.GONE);
                }
            } catch (Exception e) {
            }
            try {
                if (TextUtils.equals("1", pro.getShoppingGroupStatus())) {//是否团购 0 否 1 是
                    tag_pintuan.setVisibility(View.VISIBLE);
                } else {
                    tag_pintuan.setVisibility(View.GONE);
                }
            } catch (Exception e) {
            }
            try {
                if (TextUtils.equals("1", pro.getIntervalStatus())) {//是否区间价格 0 否 1 是
                    tag_qujianjia.setVisibility(View.VISIBLE);
                } else {
                    tag_qujianjia.setVisibility(View.GONE);
                }
            } catch (Exception e) {
            }
            try {
                if (TextUtils.equals("1", pro.getCustomizedStatus())) {//是否预订 0 否 1 是
                    tag_yuding.setVisibility(View.VISIBLE);
                } else {
                    tag_yuding.setVisibility(View.GONE);
                }
            } catch (Exception e) {
            }
            try {
                if (TextUtils.equals("1", pro.getGoodSalesModel())) {//销售模式（1：预售、2：现货)
                    tag_yushou.setVisibility(View.VISIBLE);
                } else {
                    tag_yushou.setVisibility(View.INVISIBLE);
                }
            } catch (Exception e) {
            }

        }
    }

    private void getFootPrint() {
        showProgress();
        APIRetrofit.getDefault().getMineFootprints(AppHolder.instance().getUser().getAccountId())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SubscriberImpl<BaseResult<List<ZujiBean>>>() {
                    @Override
                    public void onError(Throwable e) {
                        recyclerView.refreshComplete(20);
                        hideProgress();
                        ToastUtils.showShort("获取足迹列表失败");
                    }

                    @Override
                    public void onNext(BaseResult<List<ZujiBean>> listBaseResult) {
                        recyclerView.refreshComplete(20);
                        hideProgress();
                        if (listBaseResult.isSuccess()) {
                            zujiBeans = listBaseResult.getData();
                            if (isDataEmpty(zujiBeans)) {
                                showEmptyView(skipId);
                            } else {
                                adapter.setDataList(listBaseResult.getData());
                                showContent(null);
                            }
                        } else {
                            ToastUtils.showShort(listBaseResult.getMsg());
                        }
                    }
                });
    }

}
